home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <malloc.h>
- #include "datarec.h"
- #define MAX_DR_ENTRIES 63 /* nbr data pairs (x,f(x)) per data record */
-
-
- /*--------------------------------------------------*/
- /* store_data(flush,ofp,x,fx) - creates DAN compatible data files. Caller
- must have previously set file name in 'output_file' data
- structure and reset the file open flg in that structure.
- enter: flush=0 => put data pt in output bfr & write data rec if full
- =1 => flush final bfr, close file, and free bfrs
- =2 => store last data pt and then flush, etc.
- ofp = ptr to 'output_file' structure for data file
- x = independent variable value
- fx = dependent variable value
- */
-
- void store_data(flush,ofp,x,fx)
- int flush; /* 0=normal call, 1=final call */
- OF *ofp; /* output file ptr */
- double x,fx; /* data values for cur pt */
- {
- char msg[80]; /* err msg assembly bfr */
- int cnt; /* nbr data pts in output bfr */
- DR *bfp; /* output bfr ptr */
-
-
-
- if ((*ofp).open==0) { /* have we opened the output file? */
- /* no - allocate space for output bfr */
- bfp = (*ofp).bfr = (DR *)malloc(sizeof(DR));
- /* exit if no space available - try again nxt time */
- if (bfp==NULL) return; /* means may miss some data pts */
- sd10:
- (*ofp).fid = fopen((*ofp).name,"w+b");
- if ((*ofp).fid == NULL) { /* was open OK? */
- sprintf(msg,"Unable to open output file: %s\nRetry? (y/n) ",(*ofp).name);
- if ( toupper(prompt(msg))=='Y') goto sd10;
- (*ofp).open = 2; /* indic file should be ignored */
- return;
- }
- (*ofp).open = 1; /* indic successful open */
- (*bfp).dr_type = 7; /* set data record type */
- (*bfp).dr_pts = 0; /* no data pts yet */
- (*bfp).dr_rec_num = 1; /* record number 1 */
- }
- if ((*ofp).open > 1) return;/* exit immediately if should ignore file */
- bfp = (*ofp).bfr; /* set ptr to output bfr */
- cnt = (*bfp).dr_pts; /* nbr data pts stored in bfr */
- if (flush != 1) { /* put data pt in bfr if not 'flush only' call */
- (*bfp).dp[cnt].x = x; /* put data pt in output bfr */
- (*bfp).dp[cnt].fx = fx;
- (*bfp).dr_pts = ++cnt; /* incr nbr pts in bfr */
- }
- if (cnt >= MAX_DR_ENTRIES || flush) { /* full? or last call? */
- /* yes - output data record */
- sd20: cnt = fwrite(bfp,sizeof(DR),1,(*ofp).fid);
- if (cnt != 1) { /* had write error? */
- sprintf(msg,"Write error for output file: %s\nRetry? (y/n) ",(*ofp).name);
- clearerr((*ofp).fid);/* clear file error status */
- if ( toupper(prompt(msg))=='Y') goto sd20;
- flush = 1; /* force close now */
- (*ofp).open = 3; /* indic file should be ignored */
- }
- (*bfp).dr_pts = 0; /* reset to empty */
- if (flush) {
- fclose((*ofp).fid); /* close file on final call */
- free((*ofp).bfr); /* free the data bfr */
- free(ofp); /* free the output file structure */
- }
- }
- return;
- }
-
-
-
- /*--------------------------------------------------*/
- /* prompt(msg) - output message to user and wait for response.
- Returns response (as an integer).
- */
-
- prompt(msg)
- char *msg; /* msg to output */
- {
- printf("%s",msg);
- return getch(); /* return user's response to prompt msg */
- }
-